﻿namespace Arms.Framework
{
    using System;
    using System.Linq;

    public static class DateTimeExtensions
    {
        /// <summary>
        /// Formats this DateTime
        /// </summary>
        /// <param name="source"></param>
        /// <param name="formatString">The formatting style. Default format (Jan 01, 2012)</param>
        /// <returns>Formatted date string with date and time</returns>
        public static string ToDateFormat(this DateTime source, string formatString = "MMM dd, yyyy")
        {
            if (source.Year < 1900)
            {
                return string.Empty;
            }

            return source.ToDateTimeFormat(formatString);
        }
        /// <summary>
        /// Formats this DateTime
        /// </summary>
        /// <param name="source"></param>
        /// <param name="formatString">The formatting style. Default format (Jan 01, 2012)</param>
        /// <returns>Formatted date string with date and time or string.Empty if null</returns>
        public static string ToDateFormat(this DateTime? source, string formatString = "MMM dd, yyyy")
        {
            if (source.HasValue)
            {
                return source.Value.ToDateTimeFormat(formatString);
            }

            return string.Empty;
        }
        /// <summary>
        /// Formats this DateTime
        /// </summary>
        /// <param name="source"></param>
        /// <param name="formatString">The formatting style. Default format (Jan 01, 2012 12:56PM)</param>
        /// <returns>Formatted date string with date and time</returns>
        public static string ToDateTimeFormat(this DateTime source, string formatString = "MMM dd, yyyy hh:mmtt")
        {
            return source.ToString(formatString);
        }
        /// <summary>
        /// Formats this nullable DateTime
        /// </summary>
        /// <param name="source"></param>
        /// <param name="formatString">The formatting style. Default format (Jan 01, 2012 12:56PM)</param>
        /// <returns>Formatted date string with date and time or string.Empty if null</returns>
        public static string ToDateTimeFormat(this DateTime? source, string formatString = "MMM dd, yyyy hh:mmtt")
        {
            if (source.HasValue)
            {
                return source.Value.ToDateTimeFormat(formatString);
            }

            return string.Empty;
        }
        /// <summary>
        /// Formats this DateTime to 24 hour time
        /// </summary>
        /// <param name="source"></param>
        /// <returns>Formatted date string</returns>
        public static string To24HourTimeFormat(this DateTime source)
        {
            string formatString = "HH:mm";
            return source.ToDateTimeFormat(formatString);
        }
        /// <summary>
        /// Formats this nullable DateTime to 24 hour time
        /// </summary>
        /// <param name="source"></param>
        /// <returns>Formatted date string or string.Empty if null</returns>
        public static string To24HourTimeFormat(this DateTime? source)
        {
            if (source.HasValue)
            {
                return source.To24HourTimeFormat();
            }

            return string.Empty;
        }
        /// <summary>
        /// Converts this date time to the beginning of the day.
        /// Eg. 2009-12-06 23:13:56 becomes 2009-12-06 00:00:00
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static DateTime Normalize(this DateTime source)
        {
            return source.Date;
        }
        /// <summary>
        /// Converts this date time to the beginning of the day
        /// Eg. 2009-12-06 23:13:56 becomes 2009-12-06 00:00:00
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static DateTime? Normalize(this DateTime? source)
        {
            if (source.HasValue)
            {
                return source.Value.Normalize();
            }

            return null;
        }
    }
}